package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.MallHomeRankDTO;
import com.cheer.dto.OaRankQueryDTO;
import com.cheer.dto.OaUserQueryDTO;
import com.cheer.entity.OaUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaUserDao extends BaseMapper<OaUserEntity> {

    default Integer selectIdByOpenid(String openid) {
        OaUserEntity user = selectOne(new QueryWrapper<OaUserEntity>()
                .select("id")
                .eq("openid", openid)
                .last("limit 1"));
        if (user != null) {
            return user.getId();
        }
        return null;
    }

    default String selectUsernameById(Integer id) {
        OaUserEntity user = selectOne(new QueryWrapper<OaUserEntity>()
                .select("name")
                .eq("id", id));
        if (user != null) {
            return user.getName();
        }
        return null;
    }

    default Integer selectUserIntegralById(Integer id) {
        OaUserEntity user = selectOne(new QueryWrapper<OaUserEntity>()
                .select("use_integral")
                .eq("id", id));
        if (user != null) {
            return user.getUseIntegral();
        }
        return null;
    }

    default OaUserEntity selectIntegralById(Integer id) {
        return selectOne(new QueryWrapper<OaUserEntity>()
                .select("openid", "integral", "use_integral")
                .eq("id", id));
    }

    default IPage<OaUserEntity> selectPageByQueryCondition(OaUserQueryDTO userQueryDTO) {
        QueryWrapper<OaUserEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(userQueryDTO.getName())) {
            queryWrapper.like("name", userQueryDTO.getName());
        }

        if (StringUtils.isNotBlank(userQueryDTO.getNickname())) {
            queryWrapper.like("nickname", userQueryDTO.getNickname());
        }

        if (StringUtils.isNotBlank(userQueryDTO.getPath())) {
            queryWrapper.likeRight("path", userQueryDTO.getPath());
        }

        return selectPage(new Page<>(userQueryDTO.getPageNum(), userQueryDTO.getPageSize()), queryWrapper);
    }

    default String selectOpenidById(Integer id) {
        OaUserEntity user = selectOne(new QueryWrapper<OaUserEntity>()
                .select("openid")
                .eq("id", id));
        if (user != null) {
            return user.getOpenid();
        }
        return null;
    }

    default IPage<OaUserEntity> selectRankPage(OaRankQueryDTO rankQueryDTO) {
        QueryWrapper<OaUserEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("id");

        if (StringUtils.isNotBlank(rankQueryDTO.getType())) {
            queryWrapper.eq("type", rankQueryDTO.getType());
        }

        if (StringUtils.isNotBlank(rankQueryDTO.getPath())) {
            queryWrapper.likeRight("path", rankQueryDTO.getPath());
        }

        queryWrapper.orderByDesc("integral");

        return selectPage(new Page<>(rankQueryDTO.getPageNum(), rankQueryDTO.getPageSize()), queryWrapper);
    }

    default List<OaUserEntity> selectRankList(OaRankQueryDTO rankQueryDTO) {
        QueryWrapper<OaUserEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(rankQueryDTO.getType())) {
            queryWrapper.eq("type", rankQueryDTO.getType());
        }

        if (StringUtils.isNotBlank(rankQueryDTO.getPath())) {
            queryWrapper.likeRight("path", rankQueryDTO.getPath());
        }

        queryWrapper.orderByDesc("integral");

        return selectList(queryWrapper);
    }

    default List<OaUserEntity> selectListByOpenids(List<String> openids) {
        return selectList(new QueryWrapper<OaUserEntity>()
                .in("openid", openids));
    }

    default List<OaUserEntity> selectListByQueryCondition(OaUserQueryDTO userQueryDTO) {
        QueryWrapper<OaUserEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(userQueryDTO.getName())) {
            queryWrapper.like("name", userQueryDTO.getName());
        }

        if (StringUtils.isNotBlank(userQueryDTO.getNickname())) {
            queryWrapper.like("nickname", userQueryDTO.getNickname());
        }

        if (StringUtils.isNotBlank(userQueryDTO.getPath())) {
            queryWrapper.likeRight("path", userQueryDTO.getPath());
        }

        return selectList(queryWrapper);
    }

    default List<OaUserEntity> selectListByIntegralLimit(int start, int offset) {
        return selectList(new QueryWrapper<OaUserEntity>()
                .orderByDesc("integral")
                .last("limit " + start + "," + offset));
    }

    default List<OaUserEntity> selectListByTypeLimit(int start, int offset, String type) {
        return selectList(new QueryWrapper<OaUserEntity>()
                .eq("type", type)
                .orderByDesc("integral")
                .last("limit " + start + "," + offset));
    }

    default Integer selectCountByIntegralGe(Integer integral) {
        return selectCount(new QueryWrapper<OaUserEntity>()
                .gt("integral", integral));
    }

    Integer selectCountByTypeGe(@Param("integral") Integer integral, @Param("type") String type);


    default OaUserEntity selectByOpenid(String openid) {
        return selectOne(new QueryWrapper<OaUserEntity>()
                .eq("openid", openid)
                .last("limit 1"));
    }

    void updateIntegral(@Param("id") Integer id, @Param("credit") Integer credit);

    default String selectUpdateTimeById(Integer id) {
        OaUserEntity user = selectOne(new QueryWrapper<OaUserEntity>()
                .select("updata_time")
                .eq("id", id));
        if (user != null) {
            return user.getUpdataTime();
        }
        return null;
    }

    default void updatePathByDepartment(Integer department, String path) {
        OaUserEntity user = new OaUserEntity()
                .setPath(path);
        update(user, new QueryWrapper<OaUserEntity>()
                .eq("department", String.valueOf(department)));
    }


}
